package com.ruoyi.web.controller.basic;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.MailRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BasicEmailConfig;
import com.ruoyi.system.service.IBasicEmailConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 邮箱配置Controller
 * 
 * @author ruoyi
 * @date 2025-03-02
 */
@RestController
@RequestMapping("/system/basicEmailConfig")
public class BasicEmailConfigController extends BaseController
{
    @Autowired
    private IBasicEmailConfigService basicEmailConfigService;

    /**
     * 测试连接邮件服务器
     * @return
     */
    @GetMapping("/sendMailServer")
    public AjaxResult testMailServer(@RequestBody @Validated MailRequest mailRequest) {
        if(mailRequest == null || mailRequest.getEmailConfigId() == null) return AjaxResult.error("参数错误");
        if(basicEmailConfigService.testMailServer(mailRequest)) {
            return AjaxResult.success("连接成功");
        }
        return AjaxResult.error("连接失败");
    }

    /**
     * 查询邮箱配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:basicEmailConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasicEmailConfig basicEmailConfig)
    {
        startPage();
        List<BasicEmailConfig> list = basicEmailConfigService.selectBasicEmailConfigList(basicEmailConfig);
        return getDataTable(list);
    }

    /**
     * 导出邮箱配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:basicEmailConfig:export')")
    @Log(title = "邮箱配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasicEmailConfig basicEmailConfig)
    {
        List<BasicEmailConfig> list = basicEmailConfigService.selectBasicEmailConfigList(basicEmailConfig);
        ExcelUtil<BasicEmailConfig> util = new ExcelUtil<BasicEmailConfig>(BasicEmailConfig.class);
        util.exportExcel(response, list, "邮箱配置数据");
    }

    /**
     * 获取邮箱配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:basicEmailConfig:query')")
    @GetMapping(value = "/{emailConfigId}")
    public AjaxResult getInfo(@PathVariable("emailConfigId") Long emailConfigId)
    {
        return success(basicEmailConfigService.selectBasicEmailConfigByEmailConfigId(emailConfigId));
    }

    /**
     * 新增邮箱配置
     */
    @PreAuthorize("@ss.hasPermi('system:basicEmailConfig:add')")
    @Log(title = "邮箱配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasicEmailConfig basicEmailConfig)
    {
        return toAjax(basicEmailConfigService.insertBasicEmailConfig(basicEmailConfig));
    }

    /**
     * 修改邮箱配置
     */
    @PreAuthorize("@ss.hasPermi('system:basicEmailConfig:edit')")
    @Log(title = "邮箱配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasicEmailConfig basicEmailConfig)
    {
        return toAjax(basicEmailConfigService.updateBasicEmailConfig(basicEmailConfig));
    }

    /**
     * 删除邮箱配置
     */
    @PreAuthorize("@ss.hasPermi('system:basicEmailConfig:remove')")
    @Log(title = "邮箱配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{emailConfigIds}")
    public AjaxResult remove(@PathVariable Long[] emailConfigIds)
    {
        return toAjax(basicEmailConfigService.deleteBasicEmailConfigByEmailConfigIds(emailConfigIds));
    }
}
